package com.steptime.framework.spring.ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
 * <ol>
 *     <li>两种spring容器：ClassPathXmlApplicationContext和FileSystemXmlApplicationContext</li>
 *     <li>实例化Bean的三种方法：构造器、静态工厂、实例工厂</li>
 *     <li>spring的生命周期方法(初始化和摧毁回调方法)</li>
 *     <li>spring bean的作用域(默认为singleton)</li>
 * </ol>
 */
public class SpringBeanTest {

    @Test
    public void getBean(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/application.xml");
        Bean bean = (Bean) applicationContext.getBean("bean1");
        bean.print();
    }


    @Test
    public void getBeanByFileSystem(){
        //使用Spring的工厂
        ApplicationContext applicationContext =new FileSystemXmlApplicationContext( "d:\\application.xml");
        //通过工厂获得类
        Bean orderService =(Bean)applicationContext.getBean("bean1");
        orderService.print();
    }

    /**
     * bean的作用域设置为prototype后，每次通过容器getBean时都会新创建一个bean
     */
    @Test
    public void beanScopeTest(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/application.xml");
        System.out.println("=====================");
        User bean = (User) applicationContext.getBean("user");
        bean.print();
        User bean1 = (User) applicationContext.getBean("user");
        bean1.print();
    }

}
